package o2sjsc.projects.erp;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import o2sjsc.core.libs.json.JSONParser;
import o2sjsc.core.libs.soap.SoapUtils;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleAdapter;

public class FragKH extends Fragment implements SearchView.OnQueryTextListener {
	private static final String URL = "http://erp.o2s.vn/WebServices/ERPWebService.asmx";
	private static final String METHOD_NAME = "MGetCustomers";
	private static final String CUST_NAME = "FULL_NAME";
	private static final String CUST_MOBILE = "MOBILE";
	private static final String CUST_EMAIL = "EMAIL";
	private static final String CUST_ADDRESS = "ADDRESS_1";
	private static final String CUST_WEBSITE = "WEBSITE";
	ListAdapter adapter;
	ArrayList<HashMap<String, String>> jsonlist = null;
	HashMap<String, String> hash;
	ListView lv;
	View V;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		setHasOptionsMenu(true);
		hash = new HashMap<String, String>();
		hash.put("username", "kiennt");
		hash.put("fullname", "");
		hash.put("address", "");
		hash.put("mobile", "");
		hash.put("email", "");
		hash.put("startIndex", "1");
		hash.put("maxRecords", "10");

		V = inflater.inflate(R.layout.frag_customer, container, false);
		// Get ListView object from xml
		lv = (ListView) V.findViewById(R.id.listkh);
		new MyAsyncTask().execute();
		return V;
	}

	class MyAsyncTask extends
			AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {
		ProgressDialog progress;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progress = new ProgressDialog(getActivity());
			progress.setMessage("Loading...");
			progress.show();
		}

		@Override
		protected ArrayList<HashMap<String, String>> doInBackground(
				String... params) {

			// Call webservice
			String jsonString = SoapUtils.Invoke(URL, METHOD_NAME, hash);

			try {
				JSONParser jsonParser = new JSONParser();
				JSONArray json = jsonParser.getJSONFromString(jsonString);
				jsonlist = new ArrayList<HashMap<String, String>>();
				for (int i = 0; i < json.length(); i++) {
					JSONObject c = json.getJSONObject(i);

					String name = c.getString(CUST_NAME);
					String mobile = c.getString(CUST_MOBILE);
					String email = c.getString(CUST_EMAIL);
					String address = c.getString(CUST_ADDRESS);
					String website = c.getString(CUST_WEBSITE);

					HashMap<String, String> map = new HashMap<String, String>();

					map.put(CUST_NAME, name);
					map.put(CUST_MOBILE, mobile);
					map.put(CUST_EMAIL, email);
					map.put(CUST_ADDRESS, address);
					map.put(CUST_WEBSITE, website);
					jsonlist.add(map);

				}
			} catch (Exception e) { // TODO: handle exception
				Log.e("json", "Error");
			}
			return jsonlist;
		}

		@Override
		protected void onPostExecute(ArrayList<HashMap<String, String>> jsonlist) {
			progress.dismiss();
			adapter = null;
			adapter = new SimpleAdapter(getActivity(), jsonlist,
					R.layout.list_item_customer,
					new String[] { CUST_NAME, CUST_MOBILE, CUST_EMAIL,
							CUST_ADDRESS, CUST_WEBSITE }, new int[] {
							R.id.custName, R.id.mobile, R.id.email,
							R.id.address, R.id.website });
			FragKH.this.getActivity().runOnUiThread(new Runnable() {
				@Override
				public void run() {
					lv.setAdapter(adapter);
					((BaseAdapter) adapter).notifyDataSetChanged();
					lv.refreshDrawableState();
					lv.invalidateViews();
					// FragmentTransaction ft = FragMent1.this.getActivity()
					// .getFragmentManager().beginTransaction();
					// ft.commit();
				}
			});

		}
	}

	@Override
	public boolean onQueryTextChange(String arg0) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean onQueryTextSubmit(String arg0) {
		hash = new HashMap<String, String>();
		hash.put("username", "kiennt");
		hash.put("fullname", arg0);
		hash.put("address", "");
		hash.put("mobile", "");
		hash.put("email", "");
		hash.put("startIndex", "1");
		hash.put("maxRecords", "10");
		new MyAsyncTask().execute();
		return true;
		// return false;
	}

}
